//初始化用户表
var userlist = [{
		id: '1',
		name: '张三',
		no: '10001',
		pwd: '0000',
		state: '1',
		zhiwu: '高级技工',
		phone: '15001234567',
	}, {
		id: '2',
		name: '李四',
		no: '10002',
		pwd: '0000',
		state: '1',
		zhiwu: '高级技工',
		phone: '15001234567',
	},
	{
		id: '3',
		name: '王五',
		no: '10003',
		pwd: '0000',
		state: '1',
		zhiwu: '高级技工',
		phone: '15001234568',
	},
	{
		id: '4',
		name: '刘红',
		no: '10004',
		pwd: '0000',
		state: '1',
		zhiwu: '高级技工',
		phone: '13500993322',
	}
];

//商品分类
var goodTypelist=[{
		id: '1',
		typename: '商品',
		typecode: 'shangpin', 
	},{
		id: '2',
		typename: '物资',
		typecode: 'wuzhi', 
	}]

//商品信息
var goodInfolist=[{
		id: '1',
		typeid:'1',
		name: '电影',
		guige: '集', 
		num:'100',
		sku:'',
	},{
		id: '2',
		typeid:'1',
		name: 'DVD',
		guige: '集', 
		num:'100',
		sku:'',
	},{
		id: '3',
		typeid:'2',
		name: '电视',
		guige: '台', 
		num:'100',
		sku:'',
	}]
//入库记录
var goodInputRecord=[
	/* {
		id: '1',
		goodid:'1',
		goodname: '电影1',
		guige: '集', 
		num:'10',
		cdate:'2020-02-22',
		addby:'李四',
	} */
]
//出库记录
var goodOutputRecord=[
	/* {
		id: '1',
		goodid:'1',
		goodname: '电影2',
		guige: '集', 
		num:'20',
		cdate:'2020-02-22',
		addby:'张三',
	} */
]
 
//创建数据库表
function opendb() {
	plus.sqlite.openDatabase({
		name: 'test',
		path: '_doc/test.db',
		success: function(e) {
			console.log('openDatabase success!');
		},
		fail: function(e) {
			console.log('openDatabase failed: ' + JSON.stringify(e));
		}
	});
}
//关闭数据库
function closedb() {
	plus.sqlite.closeDatabase({
		name: 'test',
		success: function(e) {
			console.log('closeDatabase success!');
		},
		fail: function(e) {
			console.log('closeDatabase failed: ' + JSON.stringify(e));
		}
	});
}
//查询数据
function searchdb() {
	plus.sqlite.selectSql({
		name: 'test',
		sql: 'select * from database',
		success: function(data) {
			console.log('selectSql success: ');
			for (var i in data) {
				console.log(data[i]);
			}
		},
		fail: function(e) {
			console.log('selectSql failed: ' + JSON.stringify(e));
		}
	});
}


//执行sql语句  -建表语句
function executesql() {
	plus.sqlite.executeSql({
		name: 'test',
		sql: 'create table if not exists database("where" CHAR(110),"location" CHAR(100),"age" INT(11))',
		success: function(e) {
			console.log('executeSql success!');
			plus.sqlite.executeSql({
				name: 'test',
				sql: "insert into database values('北京','安乐林','11')",
				success: function(e) {
					console.log('executeSql success!');
				},
				fail: function(e) {
					console.log('executeSql failed: ' + JSON.stringify(e));
				}
			});
		},
		fail: function(e) {
			console.log('executeSql failed: ' + JSON.stringify(e));
		}
	});
}

//创建用户表
function executesql_user() {
	plus.sqlite.executeSql({
		name: 'test',
		sql: 'create table if not exists c_user("id" varchar(100) ,"name" varchar(100),"no" INT(11),"pwd" varchar(12),"state" char(1),"zhiwu" varchar(50),"phone" varchar(50))',
		success: function(e) {
			//初始化用户表
			var sqls = '';
			for (var i = 0; i < userlist.length; i++) {
				var user = userlist[i];
				sqls = "insert into c_user values('" + user.id + "','" + user.name + "','" + user.no + "','" + user.pwd + "','" +
					user.state + "','" + user.zhiwu + "','" + user.phone + "');";
				plus.sqlite.executeSql({
					name: 'test',
					sql: sqls,
					success: function(e) {
						//console.log('executeSql success!');
					},
					fail: function(e) {
						console.log('executeSql failed: ' + JSON.stringify(e));
					}
				});
			}
			console.log('创建用户表 success!');
		},
		fail: function(e) {
			console.log('executeSql failed: ' + JSON.stringify(e));
		}
	});
}

//清空用户表
function truncate_user() {
	plus.sqlite.executeSql({
		name: 'test',
		sql: "delete from c_user",
		success: function(e) {
			console.log('清空用户表 success!');
		},
		fail: function(e) {
			console.log('清空用户表 failed: ' + JSON.stringify(e));
		}
	});
}



//创建商品分类表
function executesql_goodtype() {
	plus.sqlite.executeSql({
		name: 'test',
		sql: 'create table if not exists GoodType("id" varchar(1000) ,"typename" varchar(50),"typecode" varchar(100))',
		success: function(e) { 
			var sqls = '';
			for (var i = 0; i < goodTypelist.length; i++) {
				var order = goodTypelist[i];
				sqls = "insert into GoodType values('" + order.id + "','" + order.typename + "','" + order.typecode   + "');";
				plus.sqlite.executeSql({
					name: 'test',
					sql: sqls,
					success: function(e) {

					},
					fail: function(e) {
						console.log(' 表新建 failed: ' + JSON.stringify(e));
					}
				});
			}
			console.log('商品分类表新建 success!');
		},
		fail: function(e) {
			console.log('executeSql failed: ' + JSON.stringify(e));
		}
	});
}

//清空商品分类表
function truncate_goodtype() {
	plus.sqlite.executeSql({
		name: 'test',
		sql: "delete from GoodType",
		success: function(e) {
			console.log('清空商品分类表 success!');
		},
		fail: function(e) {
			console.log('清空商品分类表 failed: ' + JSON.stringify(e));
		}
	});
}


//创建商品信息表
function executesql_goodInfo() {
	plus.sqlite.executeSql({
		name: 'test',
		sql: 'create table if not exists GoodInfo("id" varchar(100) ,"typeid" varchar(100) ,"name" varchar(50),"guige" varchar(100),"num" INTEGER,"sku" varchar(50))',
		success: function(e) { 
			var sqls = '';
			for (var i = 0; i < goodInfolist.length; i++) {
				var order = goodInfolist[i];
				sqls = "insert into GoodInfo values('" + order.id + "','" + order.typeid + "','" + order.name + "','" +
					order
					.guige + "','" +
					order.num + "','" + order.sku + "');";
				plus.sqlite.executeSql({
					name: 'test',
					sql: sqls,
					success: function(e) {

					},
					fail: function(e) {
						console.log('商品信息表新建 failed: ' + JSON.stringify(e));
					}
				});
			}
			console.log('商品信息表新建 success!');
		},
		fail: function(e) {
			console.log('executeSql failed: ' + JSON.stringify(e));
		}
	});
}

//清空商品信息表
function truncate_goodInfo() {
	plus.sqlite.executeSql({
		name: 'test',
		sql: "delete from GoodInfo",
		success: function(e) {
			console.log('清空商品信息表 success!');
		},
		fail: function(e) {
			console.log('清空商品信息表 failed: ' + JSON.stringify(e));
		}
	});
}



//创建出库记录表
function executesql_goodOutputRecord() {
	plus.sqlite.executeSql({
		name: 'test',
		sql: 'create table if not exists GoodOutputRecord  ("id" varchar(100) ,"goodid" varchar(50),"goodname" varchar(100),"num" varchar(50),"addby" varchar(50),"cdate" varchar(20))',
		success: function(e) { 
			var sqls = '';
			for (var i = 0; i < goodOutputRecord.length; i++) {
				var order = goodOutputRecord[i];
				sqls = "insert into GoodOutputRecord values('" + order.id + "','" + order.goodid + "','" + order.goodname + "','" +
					order
					.num + "','" +
					order.addby + "','"  + order.cdate  + "');";
				plus.sqlite.executeSql({
					name: 'test',
					sql: sqls,
					success: function(e) {

					},
					fail: function(e) {
						console.log('出库记录表新建 failed: ' + JSON.stringify(e));
					}
				});
			}
			console.log('出库记录表新建 success!');
		},
		fail: function(e) {
			console.log('executeSql failed: ' + JSON.stringify(e));
		}
	});
}

//清空出库记录表
function truncate_goodOutputRecord() {
	plus.sqlite.executeSql({
		name: 'test',
		sql: "delete from GoodOutputRecord",
		success: function(e) {
			console.log('清空出库记录表 success!');
		},
		fail: function(e) {
			console.log('清空出库记录表 failed: ' + JSON.stringify(e));
		}
	});
}



//创建入库记录表
function executesql_goodInputRecord() {
	plus.sqlite.executeSql({
		name: 'test',
		sql: 'create table if not exists GoodInputRecord  ("id" varchar(100) ,"goodid" varchar(50),"goodname" varchar(100),"num" varchar(50),"addby" varchar(50),"cdate" varchar(20))',
		success: function(e) { 
			var sqls = '';
			for (var i = 0; i < goodInputRecord.length; i++) {
				var order = goodInputRecord[i];
				sqls = "insert into GoodInputRecord values('" + order.id + "','" + order.goodid + "','" + order.goodname + "','" +
					order
					.num + "','" +
					order.addby + "','"  + order.cdate  + "');";
				plus.sqlite.executeSql({
					name: 'test',
					sql: sqls,
					success: function(e) {

					},
					fail: function(e) {
						console.log('入库记录表新建 failed: ' + JSON.stringify(e));
					}
				});
			}
			console.log('入库记录表新建 success!');
		},
		fail: function(e) {
			console.log('executeSql failed: ' + JSON.stringify(e));
		}
	});
}

//清空入库记录表
function truncate_goodInputRecord() {
	plus.sqlite.executeSql({
		name: 'test',
		sql: "delete from GoodInputRecord",
		success: function(e) {
			console.log('清空入库记录表 success!');
		},
		fail: function(e) {
			console.log('清空入库记录表 failed: ' + JSON.stringify(e));
		}
	});
}


 

export default {
	opendb,
	closedb,
	executesql_user,truncate_user,
	executesql_goodtype,truncate_goodtype,
	executesql_goodInfo,truncate_goodInfo,
	executesql_goodOutputRecord,truncate_goodOutputRecord,
	executesql_goodInputRecord,truncate_goodInputRecord,
	 
}
